﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data.Sql;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.Configuration;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// TODO: Fill this in.
/// </summary>
public partial class Create : System.Web.UI.Page
{
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Validate();
        if (Page.IsValid)
        {
            string connectionString =
                WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            string creator = txtCreator.Text;

            string label = txtLabel.Text;

            int categoryId = Convert.ToInt32(ddlCategory.SelectedValue);

            string priorityCode = ddlPriority.SelectedItem.Text;

            string assignee = ddlAssignment.SelectedValue;

            DateTime createdDate = DateTime.Now;

            string description = txtDescription.Text;

            SqlConnection con = new SqlConnection(connectionString);

            using (con)
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "sprocInsertIssue";

                try
                {
                    cmd.Parameters.Add(new SqlParameter("@cat_id", SqlDbType.Int));
                    cmd.Parameters["@cat_id"].Value = categoryId;
                    cmd.Parameters.Add(new SqlParameter("@priority_code", SqlDbType.VarChar, 20));
                    cmd.Parameters["@priority_code"].Value = priorityCode;
                    cmd.Parameters.Add(new SqlParameter("@assign_id", SqlDbType.VarChar, 20));
                    cmd.Parameters["@assign_id"].Value = assignee;
                    cmd.Parameters.Add(new SqlParameter("@iss_creatorname", SqlDbType.VarChar, 50));
                    cmd.Parameters["@iss_creatorname"].Value = creator;
                    cmd.Parameters.Add(new SqlParameter("@iss_label", SqlDbType.VarChar, 128));
                    cmd.Parameters["@iss_label"].Value = label;
                    cmd.Parameters.Add(new SqlParameter("@iss_desc", SqlDbType.VarChar, 1000));
                    cmd.Parameters["@iss_desc"].Value = description;
                    cmd.Parameters.Add(new SqlParameter("@iss_datecreated", SqlDbType.DateTime));
                    cmd.Parameters["@iss_datecreated"].Value = createdDate;
                    cmd.Parameters.Add(new SqlParameter("@iss_dateresolved", SqlDbType.DateTime));
                    cmd.Parameters["@iss_dateresolved"].Value = null;

                    con.Open();
                    cmd.ExecuteNonQuery();

                    Session["AddMessage"] = "Issue successfully added to the database.";
                    Response.Redirect("~/View.aspx", true);
                }
                catch
                {
                    lblStatus.Text = "There was an error creating this issue.<br />";
                    lblStatus.Visible = true;
                }
            }
        }
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Create.aspx");
    }
}
